<?php


abstract class BasesfSejour extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $patient_id;


	
	protected $chambre_id;


	
	protected $motif;


	
	protected $date_debut;


	
	protected $date_fin;


	
	protected $remarques;

	
	protected $asfPatient;

	
	protected $asfChambre;

	
	protected $collsfInterventions;

	
	protected $lastsfInterventionCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getPatientId()
	{

		return $this->patient_id;
	}

	
	public function getChambreId()
	{

		return $this->chambre_id;
	}

	
	public function getMotif()
	{

		return $this->motif;
	}

	
	public function getDateDebut($format = 'Y-m-d')
	{

		if ($this->date_debut === null || $this->date_debut === '') {
			return null;
		} elseif (!is_int($this->date_debut)) {
						$ts = strtotime($this->date_debut);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [date_debut] as date/time value: " . var_export($this->date_debut, true));
			}
		} else {
			$ts = $this->date_debut;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getDateFin($format = 'Y-m-d')
	{

		if ($this->date_fin === null || $this->date_fin === '') {
			return null;
		} elseif (!is_int($this->date_fin)) {
						$ts = strtotime($this->date_fin);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [date_fin] as date/time value: " . var_export($this->date_fin, true));
			}
		} else {
			$ts = $this->date_fin;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getRemarques()
	{

		return $this->remarques;
	}

	
	public function setId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = sfSejourPeer::ID;
		}

	} 
	
	public function setPatientId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->patient_id !== $v) {
			$this->patient_id = $v;
			$this->modifiedColumns[] = sfSejourPeer::PATIENT_ID;
		}

		if ($this->asfPatient !== null && $this->asfPatient->getId() !== $v) {
			$this->asfPatient = null;
		}

	} 
	
	public function setChambreId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->chambre_id !== $v) {
			$this->chambre_id = $v;
			$this->modifiedColumns[] = sfSejourPeer::CHAMBRE_ID;
		}

		if ($this->asfChambre !== null && $this->asfChambre->getId() !== $v) {
			$this->asfChambre = null;
		}

	} 
	
	public function setMotif($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->motif !== $v) {
			$this->motif = $v;
			$this->modifiedColumns[] = sfSejourPeer::MOTIF;
		}

	} 
	
	public function setDateDebut($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [date_debut] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->date_debut !== $ts) {
			$this->date_debut = $ts;
			$this->modifiedColumns[] = sfSejourPeer::DATE_DEBUT;
		}

	} 
	
	public function setDateFin($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [date_fin] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->date_fin !== $ts) {
			$this->date_fin = $ts;
			$this->modifiedColumns[] = sfSejourPeer::DATE_FIN;
		}

	} 
	
	public function setRemarques($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->remarques !== $v) {
			$this->remarques = $v;
			$this->modifiedColumns[] = sfSejourPeer::REMARQUES;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->patient_id = $rs->getInt($startcol + 1);

			$this->chambre_id = $rs->getInt($startcol + 2);

			$this->motif = $rs->getString($startcol + 3);

			$this->date_debut = $rs->getDate($startcol + 4, null);

			$this->date_fin = $rs->getDate($startcol + 5, null);

			$this->remarques = $rs->getString($startcol + 6);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 7; 
		} catch (Exception $e) {
			throw new PropelException("Error populating sfSejour object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(sfSejourPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			sfSejourPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(sfSejourPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


												
			if ($this->asfPatient !== null) {
				if ($this->asfPatient->isModified()) {
					$affectedRows += $this->asfPatient->save($con);
				}
				$this->setsfPatient($this->asfPatient);
			}

			if ($this->asfChambre !== null) {
				if ($this->asfChambre->isModified()) {
					$affectedRows += $this->asfChambre->save($con);
				}
				$this->setsfChambre($this->asfChambre);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = sfSejourPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += sfSejourPeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collsfInterventions !== null) {
				foreach($this->collsfInterventions as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->asfPatient !== null) {
				if (!$this->asfPatient->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->asfPatient->getValidationFailures());
				}
			}

			if ($this->asfChambre !== null) {
				if (!$this->asfChambre->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->asfChambre->getValidationFailures());
				}
			}


			if (($retval = sfSejourPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collsfInterventions !== null) {
					foreach($this->collsfInterventions as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = sfSejourPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getPatientId();
				break;
			case 2:
				return $this->getChambreId();
				break;
			case 3:
				return $this->getMotif();
				break;
			case 4:
				return $this->getDateDebut();
				break;
			case 5:
				return $this->getDateFin();
				break;
			case 6:
				return $this->getRemarques();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = sfSejourPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getPatientId(),
			$keys[2] => $this->getChambreId(),
			$keys[3] => $this->getMotif(),
			$keys[4] => $this->getDateDebut(),
			$keys[5] => $this->getDateFin(),
			$keys[6] => $this->getRemarques(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = sfSejourPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setPatientId($value);
				break;
			case 2:
				$this->setChambreId($value);
				break;
			case 3:
				$this->setMotif($value);
				break;
			case 4:
				$this->setDateDebut($value);
				break;
			case 5:
				$this->setDateFin($value);
				break;
			case 6:
				$this->setRemarques($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = sfSejourPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setPatientId($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setChambreId($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setMotif($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setDateDebut($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setDateFin($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setRemarques($arr[$keys[6]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(sfSejourPeer::DATABASE_NAME);

		if ($this->isColumnModified(sfSejourPeer::ID)) $criteria->add(sfSejourPeer::ID, $this->id);
		if ($this->isColumnModified(sfSejourPeer::PATIENT_ID)) $criteria->add(sfSejourPeer::PATIENT_ID, $this->patient_id);
		if ($this->isColumnModified(sfSejourPeer::CHAMBRE_ID)) $criteria->add(sfSejourPeer::CHAMBRE_ID, $this->chambre_id);
		if ($this->isColumnModified(sfSejourPeer::MOTIF)) $criteria->add(sfSejourPeer::MOTIF, $this->motif);
		if ($this->isColumnModified(sfSejourPeer::DATE_DEBUT)) $criteria->add(sfSejourPeer::DATE_DEBUT, $this->date_debut);
		if ($this->isColumnModified(sfSejourPeer::DATE_FIN)) $criteria->add(sfSejourPeer::DATE_FIN, $this->date_fin);
		if ($this->isColumnModified(sfSejourPeer::REMARQUES)) $criteria->add(sfSejourPeer::REMARQUES, $this->remarques);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(sfSejourPeer::DATABASE_NAME);

		$criteria->add(sfSejourPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setPatientId($this->patient_id);

		$copyObj->setChambreId($this->chambre_id);

		$copyObj->setMotif($this->motif);

		$copyObj->setDateDebut($this->date_debut);

		$copyObj->setDateFin($this->date_fin);

		$copyObj->setRemarques($this->remarques);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getsfInterventions() as $relObj) {
				$copyObj->addsfIntervention($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new sfSejourPeer();
		}
		return self::$peer;
	}

	
	public function setsfPatient($v)
	{


		if ($v === null) {
			$this->setPatientId(NULL);
		} else {
			$this->setPatientId($v->getId());
		}


		$this->asfPatient = $v;
	}


	
	public function getsfPatient($con = null)
	{
		if ($this->asfPatient === null && ($this->patient_id !== null)) {
						include_once 'lib/model/om/BasesfPatientPeer.php';

			$this->asfPatient = sfPatientPeer::retrieveByPK($this->patient_id, $con);

			
		}
		return $this->asfPatient;
	}

	
	public function setsfChambre($v)
	{


		if ($v === null) {
			$this->setChambreId(NULL);
		} else {
			$this->setChambreId($v->getId());
		}


		$this->asfChambre = $v;
	}


	
	public function getsfChambre($con = null)
	{
		if ($this->asfChambre === null && ($this->chambre_id !== null)) {
						include_once 'lib/model/om/BasesfChambrePeer.php';

			$this->asfChambre = sfChambrePeer::retrieveByPK($this->chambre_id, $con);

			
		}
		return $this->asfChambre;
	}

	
	public function initsfInterventions()
	{
		if ($this->collsfInterventions === null) {
			$this->collsfInterventions = array();
		}
	}

	
	public function getsfInterventions($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BasesfInterventionPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collsfInterventions === null) {
			if ($this->isNew()) {
			   $this->collsfInterventions = array();
			} else {

				$criteria->add(sfInterventionPeer::SEJOUR_ID, $this->getId());

				sfInterventionPeer::addSelectColumns($criteria);
				$this->collsfInterventions = sfInterventionPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(sfInterventionPeer::SEJOUR_ID, $this->getId());

				sfInterventionPeer::addSelectColumns($criteria);
				if (!isset($this->lastsfInterventionCriteria) || !$this->lastsfInterventionCriteria->equals($criteria)) {
					$this->collsfInterventions = sfInterventionPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastsfInterventionCriteria = $criteria;
		return $this->collsfInterventions;
	}

	
	public function countsfInterventions($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BasesfInterventionPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(sfInterventionPeer::SEJOUR_ID, $this->getId());

		return sfInterventionPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addsfIntervention(sfIntervention $l)
	{
		$this->collsfInterventions[] = $l;
		$l->setsfSejour($this);
	}

} 